<?php
include_once 'MY/extra_sql.php';
class CompanyController extends Zend_Controller_Action{
	function init(){
		if($_SESSION['user']==""){
			echo "<script>window.location='../user/login';</script>"; exit;
		}
		$this->registry = Zend_Registry::getInstance();
		$this->view = $this->registry['view'];
		$this->view->baseUrl = $this->_request->getBaseUrl();
		$this->view->template = "company";
		$this->view->conf = new Zend_Config(require './application/configs/config.php');
		$group_id = $_SESSION['user']['group_id'];
		$group = new Group();
		$group = $group->getgroupByid($group_id);
		$groups = explode(',',$group[0]['models']);
		if(!in_array('company',$groups)){
			echo "<script>alert('No Permission To Enter!');history.go('-1');</script>";exit();
		}
	}

	public function indexAction(){
		$filter=new Zend_Filter_StripTags();
		$company = new Company();
		$db = new mysql(constant('dbhost'),constant('dbuser'),constant('dbpass'),constant('dbname'),"UTF8");
		$page=$filter->filter(($this->_request->getParam('page')));
		$companys = $company->getAllCompanys('','','','');
		
		$arrDate = getdate();
		$year = $arrDate['year']+1;
		$time = strtotime($year.'-01-01');
		$arrTime = array();
		for ($i=0;$i<8;$i++) {
			$arrTime[] = array(strtotime(($year-1).'-01-01'), strtotime(($year).'-01-01'));
			$year--;
			$arrYear[] = $year;
		}
		
		$arrProject = array();
		foreach($arrTime as $key=>$row){
			$arrProject = $db->select_company_countByyear($row[0] ,$row[1]);

			foreach($arrProject as $k=>$v){
		
				$company_count[$key]['count']= $v['count'];
			}
			$company_count[$key]['year'] = $arrYear[$key];
		}
        $this->view->company_count = $company_count;
		$this->view->top10companys = $db->selecttopcompany();
		$this->view->total = count($companys);
		$this->view->top = $company->getTopCompanys();
		if ($page=="") {
			$page="1";
		}
		//总条数
		$total = count($companys);
		//总页数
		$perpage = "10";
		$pages = ceil($total/$perpage);
		$this->view->companys = $company->getAllcompanys($page,$perpage,'','');
		$this->view->company = $companys;
		$this->view->page = $page;
		$this->view->pages = $pages;
		echo $this->view->render('company/index.html');
	}

	//添加公司方法
	public function addAction(){
		$filter=new Zend_Filter_StripTags();
		$company = new Company();
		$operation = new Operation();
		if (strtolower($_SERVER['REQUEST_METHOD'])=='post') {
			$name=$filter->filter(($this->_request->getPost('name')));
			$category=$filter->filter(($this->_request->getPost('category')));
			$city=$filter->filter(($this->_request->getPost('city')));
			$location=$filter->filter($this->_request->getPost('location'));
			$summary=$this->_request->getPost('summary');
			$postcode=$filter->filter(($this->_request->getPost('postcode')));
			$pid=$filter->filter(($this->_request->getPost('pid')));
			$mark=$filter->filter($this->_request->getPost('mark'));
			$short_name=$filter->filter($this->_request->getPost('short_name'));
			$staff_num=$filter->filter($this->_request->getPost('staff_num'));
			$value_num=$filter->filter($this->_request->getPost('value_num'));
			$manager=$filter->filter($this->_request->getPost('manager'));
			$ass_manager=$filter->filter($this->_request->getPost('ass_manager'));
			$lss_manager=$filter->filter($this->_request->getPost('lss_manager'));
			$bb_name=$filter->filter($this->_request->getPost('bb_name'));
			$linkman=$filter->filter($this->_request->getPost('linkman'));
			$phone=$filter->filter($this->_request->getPost('phone'));
			$department_phone=$filter->filter($this->_request->getPost('department_phone'));
			$fox=$filter->filter($this->_request->getPost('fox'));
			$user_id=$_SESSION['user']['user_id'];
			$data = array(
				'name'=>$name,
			    'category'=>$category,
				'mark'=>$mark,
				'short_name'=>$short_name,
				'staff_num'=>$staff_num,
				'value_num'=>$value_num,
				'manager'=>$manager,
				'ass_manager'=>$ass_manager,
				'lss_manager'=>$lss_manager,
				'bb_name'=>$bb_name,
				'linkman'=>$linkman,
				'phone'=>$phone,
				'department_phone'=>$department_phone,
				'fox'=>$fox,
				'city'=>$city,
				'location'=>$location,
				'summary'=>$summary,
				'postcode'=>$postcode,
				'pid'=>$pid,
			    'user_id'=>$user_id
			);
			$company_id = $company->insert($data);
			if ($company_id) {
				$time = time();
				$data = array(
					'op_name'=>'add',
					'model_name'=>'company',
					'user_id'=>$_SESSION['user']['user_id'],
					'user_name'=>$_SESSION['user']['user_name'],
					'name'=>'增加客户',
					'create_time'=>$time
				);
				$query = $operation->insert($data);
				echo "<script>window.location='detail?company_id=".$company_id."'</script>";
			}else{
				
			}
		}
		echo $this->view->render('company/add.html');
	}

	//修改公司方法
	public function editAction(){
	    $filter=new Zend_Filter_StripTags();
	    $company = new Company();
	    $operation = new Operation();
	    $company_id=$filter->filter(($this->_request->getParam('company_id')));
		if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
			$name=$filter->filter(($this->_request->getPost('name')));
			$category=$filter->filter(($this->_request->getPost('category')));
			$city=$filter->filter(($this->_request->getPost('city')));
			$location=$filter->filter(($this->_request->getPost('location')));
			$summary=$this->_request->getPost('summary');
			$postcode=$filter->filter(($this->_request->getPost('postcode')));
			$pid=$filter->filter(($this->_request->getPost('pid')));
			$mark=$filter->filter($this->_request->getPost('mark'));
			$short_name=$filter->filter($this->_request->getPost('short_name'));
			$staff_num=$filter->filter($this->_request->getPost('staff_num'));
			$value_num=$filter->filter($this->_request->getPost('value_num'));
			$manager=$filter->filter($this->_request->getPost('manager'));
			$ass_manager=$filter->filter($this->_request->getPost('ass_manager'));
			$lss_manager=$filter->filter($this->_request->getPost('lss_manager'));
			$bb_name=$filter->filter($this->_request->getPost('bb_name'));
			$linkman=$filter->filter($this->_request->getPost('linkman'));
			$phone=$filter->filter($this->_request->getPost('phone'));
			$department_phone=$filter->filter($this->_request->getPost('department_phone'));
			$fox=$filter->filter($this->_request->getPost('fox'));
			$data = array(
				'name'=>$name,
			    'category'=>$category,
				'mark'=>$mark,
				'short_name'=>$short_name,
				'staff_num'=>$staff_num,
				'value_num'=>$value_num,
				'manager'=>$manager,
				'ass_manager'=>$ass_manager,
				'lss_manager'=>$lss_manager,
				'bb_name'=>$bb_name,
				'linkman'=>$linkman,
				'phone'=>$phone,
				'department_phone'=>$department_phone,
				'fox'=>$fox,
				'city'=>$city,
				'location'=>$location,
				'summary'=>$summary,
				'postcode'=>$postcode,
				'pid'=>$pid
			);
			$company->updatecompanyBycompany_id($data, $company_id);
			$time = time();
			$data = array(
				'op_name'=>'edit',
				'model_name'=>'company',
				'user_id'=>$_SESSION['user']['user_id'],
				'user_name'=>$_SESSION['user']['user_name'],
				'name'=>'更新客户',
				'create_time'=>$time
			);
			$query = $operation->insert($data);
			echo "<script>window.location='detail?company_id=".$company_id."'</script>";
		} else {
	        $this->view->company=$company->getcompanyBycompany_id($company_id);
			//上级客户
	        /*$pid = $this->view->company[0]['pid'];
	        if ($pid!='0') {
	            $p_company=$company->getcompanyBycompany_id($pid);
	            $this->view->p_company_name = $p_company[0]['name'];
	        } else {
	            $this->view->p_company_name="";
	        }*/
	    }
	    echo $this->view->render('company/edit.html');
	}

	public function barsAction(){
		$filter = new Zend_Filter_StripTags();
		$project = new Project();
		$arrDate = getdate();
		$year = $arrDate['year']+1;
		$time = strtotime($year.'-01-01');
		$arrTime = array();
		for ($i=0;$i<8;$i++) {
			$arrTime[] = array(strtotime(($year-1).'-01-01'), strtotime(($year).'-01-01'));
			$year--;
			$arrYear[] = $year;
			$arrProjectType[] = array(0,0,0,0);
		}

		foreach($arrYear as $row){
			$arrYearShow[] = strval($row);
		}
		$arrProject = array();
		foreach($arrTime as $key=>$row){
			$arrProject = $project->getProjectByTime($row[0] ,$row[1]);
			foreach($arrProject as $k=>$v){
				$arrProjectType[$key][$arrProject[$k]['category']]++;
			}
		}
		$count = count($arrProjectType);
		for($i=0;$i<$count;$i++){
			foreach($arrProjectType[$i] as $key=>$row){
				$arrTypeNum[$key][] = $row;
			}
		}
		require_once('./public/chart/php-ofc-library/open-flash-chart.php');

		$title = new title( date("学员统计") );

		$arr1 = $arrTypeNum[0];
		$arr2 = $arrTypeNum[1];
		$arr3 = $arrTypeNum[2];
		$arr4 = $arrTypeNum[3];
		$arrMax = array(max($arr1),max($arr2),max($arr3),max($arr4));
		$intMax = max($arrMax);
		$y_num = ($intMax>=10)?ceil($intMax/10)*10:10;

		$bar1 = new bar_glass();
		$bar1->set_values( $arr1 );
		$bar1->set_colour('#006600');
		$bar1->set_on_show(new bar_on_show('pop-up',0.5,0.2));
		$bar1->set_tooltip( 'GB：#val#' );

		$bar2 = new bar_glass();
		$bar2->set_values( $arr2 );
		$bar2->set_colour('#000000');
		$bar2->set_on_show(new bar_on_show('pop-up',0.5,0.2));
		$bar2->set_tooltip( 'BB：#val#' );

		$bar3 = new bar_glass();
		$bar3->set_values( $arr3 );
		$bar3->set_colour('#0000ff');
		$bar3->set_on_show(new bar_on_show('pop-up',0.5,0.2));
		$bar3->set_tooltip( 'CH：#val#' );

		$bar4 = new bar_glass();
		$bar4->set_values( $arr4 );
		$bar4->set_colour('#ff00ff');
		$bar4->set_on_show(new bar_on_show('pop-up',0.5,0.2));
		$bar4->set_tooltip( 'TT：#val#' );

		$y = new y_axis();
		$y->set_range( 0, $y_num, $y_num/10 );

		$x = new x_axis();
		$x->set_labels_from_array( $arrYearShow );

		$chart = new open_flash_chart();
		$chart->set_title( $title );
		$chart->add_element( $bar1 );
		$chart->add_element( $bar2 );
		$chart->add_element( $bar3 );
		$chart->add_element( $bar4 );
		$chart->set_y_axis( $y );
		$chart->set_x_axis( $x );

		echo $chart->toPrettyString();
	}

	public function barAction(){
		$filter = new Zend_Filter_StripTags();
		$project = new Project();
		$arrDate = getdate();
		$year = $arrDate['year']+1;
		$time = strtotime($year.'-01-01');
		$arrTime = array();
		for ($i=0;$i<8;$i++) {
			$arrTime[] = array(strtotime(($year-1).'-01-01'), strtotime(($year).'-01-01'));
			$year--;
			$arrYear[] = $year;
			$arrProjectType[] = array(0,0,0,0);
		}

		foreach($arrYear as $row){
			$arrYearShow[] = strval($row);
		}
		$arrProject = array();
		foreach($arrTime as $key=>$row){
			$arrProject = $project->getProjectByTime($row[0] ,$row[1]);
			foreach($arrProject as $k=>$v){
				$arrProjectType[$key][$arrProject[$k]['category']]++;
			}
		}
		$count = count($arrProjectType);
		for($i=0;$i<$count;$i++){
			foreach($arrProjectType[$i] as $key=>$row){
				$arrTypeNum[$key][] = $row;
			}
		}
		require_once('./public/chart/php-ofc-library/open-flash-chart.php');

		$title = new title( date("项目统计") );

		$arr1 = $arrTypeNum[0];
		$arr2 = $arrTypeNum[1];
		$arr3 = $arrTypeNum[2];
		$arr4 = $arrTypeNum[3];
		$arrMax = array(max($arr1),max($arr2),max($arr3),max($arr4));
		$intMax = max($arrMax);
		$y_num = ($intMax>=10)?ceil($intMax/10)*10:10;

		$bar1 = new bar_glass();
		$bar1->set_values( $arr1 );
		$bar1->set_colour('#006600');
		$bar1->set_on_show(new bar_on_show('pop-up',0.5,0.2));
		$bar1->set_tooltip( 'GB：#val#' );

		$bar2 = new bar_glass();
		$bar2->set_values( $arr2 );
		$bar2->set_colour('#000000');
		$bar2->set_on_show(new bar_on_show('pop-up',0.5,0.2));
		$bar2->set_tooltip( 'BB：#val#' );

		$bar3 = new bar_glass();
		$bar3->set_values( $arr3 );
		$bar3->set_colour('#0000ff');
		$bar3->set_on_show(new bar_on_show('pop-up',0.5,0.2));
		$bar3->set_tooltip( 'CH：#val#' );

		$bar4 = new bar_glass();
		$bar4->set_values( $arr4 );
		$bar4->set_colour('#ff00ff');
		$bar4->set_on_show(new bar_on_show('pop-up',0.5,0.2));
		$bar4->set_tooltip( 'TT：#val#' );

		$y = new y_axis();
		$y->set_range( 0, $y_num, $y_num/10 );

		$x = new x_axis();
		$x->set_labels_from_array( $arrYearShow );

		$chart = new open_flash_chart();
		$chart->set_title( $title );
		$chart->add_element( $bar1 );
		$chart->add_element( $bar2 );
		$chart->add_element( $bar3 );
		$chart->add_element( $bar4 );
		$chart->set_y_axis( $y );
		$chart->set_x_axis( $x );

		echo $chart->toPrettyString();
	}

	//公司列表页
	public function listAction(){
		$filter=new Zend_Filter_StripTags();
		$company = new Company();
		$operation = new Operation();
		$page=$filter->filter(($this->_request->getParam('page')));
		$search_name=$filter->filter(($this->_request->getParam('search_name')));
		$search_city=$filter->filter(($this->_request->getParam('search_city')));
		if ($page=="") {
                $page="1";
		}
		$companys = $company->getAllcompanys('','',$search_name,$search_city);
		//总条数
		$total = count($companys);
		//总页数
		$perpage = "10";
		$pages = ceil($total/$perpage);
		$this->view->companys = $company->getAllcompanys($page,$perpage,$search_name,$search_city);
		$this->view->company = $companys;
		$this->view->page = $page;
		$this->view->pages = $pages;
		$this->view->total = $total;
		$time = time();
		$data = array(
			'op_name'=>'list',
			'model_name'=>'company',
			'user_id'=>$_SESSION['user']['user_id'],
			'user_name'=>$_SESSION['user']['user_name'],
			'name'=>'查看所有客户',
			'create_time'=>$time
		);
		$query = $operation->insert($data);
		echo $this->view->render('company/list.html');
	}

	//公司详情页
    public function detailAction(){
		$filter=new Zend_Filter_StripTags();
		$company = new Company();
		$student = new Student();
		$operation = new Operation();
		$studentclass = new StudentClass();
		$project = new Project();
		$db = new mysql(constant('dbhost'),constant('dbuser'),constant('dbpass'),constant('dbname'),"UTF8");
		$company_id=$filter->filter(($this->_request->getParam('company_id')));
		$page=$filter->filter(($this->_request->getParam('page')));
		$stu_page=$filter->filter(($this->_request->getParam('stu_page')));
		$pro_page=$filter->filter(($this->_request->getParam('pro_page')));
		//comment的页数
		if ($page=="") {
			$page="1";
		}
        if ($stu_page=="") {
			$stu_page="1";
		}
        if ($pro_page=="") {
			$pro_page="1";
		}

		$students = $student->getStudentBycompany_id($company_id,9999,'');
		//总条数
		$stu_total = count($students);

		//总页数
		$perpage = "10";
		$stu_pages = ceil($stu_total/$perpage);
		$stu_start = ($stu_page-1)*$perpage;
		$stu_end = $perpage;
		$this->view->stu_page = $stu_page;
		$this->view->stu_pages = $stu_pages;
		$this->view->stu_total = $stu_total;
		
		$this->view->students = $student->getStudentBycompany_id($company_id,$stu_page,$stu_end);
		$this->view->company = $company->getcompanyBycompany_id($company_id);
		
		//查询所有的comment和发表comment的用户名
		$count = $db->select_comment('','',$company_id,'4');

		//总条数
		$total = count($count);

		//总页数
		$perpage = "2";
		$pages = ceil($total/$perpage);
		$start = ($page-1)*$perpage;
		$end = $perpage;
		$this->view->page = $page;
		$this->view->pages = $pages;
		$this->view->total = $total;
		$this->view->comments = $db->select_comment($start,$end,$company_id,'4');
		$time = time();
		$data = array(
			'op_name'=>'detail',
			'model_name'=>'company',
			'user_id'=>$_SESSION['user']['user_id'],
			'user_name'=>$_SESSION['user']['user_name'],
			'name'=>'查看客户详情',
			'create_time'=>$time
		);
		$query = $operation->insert($data);
		//通过company_id，查询出所有的学生
		$this->view->studentandproject = $db->selectstudentprojectby_companyid($company_id);
        //print_r( $this->view->studentandproject );
		//通过学生再去查询出项目

		$array = array();
		for ($i=0;$i<count($students);$i++) {
			$array[].=$students[$i]['student_id'];
		}
		$projects = $db->selectstudentprojectinarray($array,9999,'');
		//总条数
		$pro_total = count($projects);

		//总页数
		$perpage = "10";
		$pro_pages = ceil($pro_total/$perpage);
		$pro_start = ($pro_page-1)*$perpage;
		$pro_end = $perpage;
		$this->view->pro_page = $pro_page;
		$this->view->pro_pages = $pro_pages;
		$this->view->pro_total = $pro_total;
		
		$this->view->projects = $db->selectstudentprojectinarray($array,$pro_start,$pro_end);
		echo $this->view->render('company/detail.html');
	}
	
	//删除公司
	public function deleteAction(){
		$filter=new Zend_Filter_StripTags();
		$company = new Company();
		$operation = new Operation();
		$company_id=$filter->filter(($this->_request->getParam('company_id')));
		$query = $company->delcompanyBycompany_id($company_id);
		if ($query) {
			$time = time();
			$data = array(
				'op_name'=>'delete',
				'model_name'=>'company',
				'user_id'=>$_SESSION['user']['user_id'],
				'user_name'=>$_SESSION['user']['user_name'],
				'name'=>'删除客户',
				'create_time'=>$time
			);
			$query = $operation->insert($data);
			echo "<script>window.location='list'</script>";
		} else {
			
		}
	}
	
	public function hoverAction(){
		$filter=new Zend_Filter_StripTags();
		$company = new Company();
		$search_name=$filter->filter(($this->_request->getParam('search_name')));
	    $this->view->companys = $company->getAllcompanys('','',$search_name,'');
		echo $this->view->render('company/hover.html');
	}
}
?>
